#!/bin/env python3
# -*- coding:utf-8 -*-
"""
    [模块名]
    Add By : cdj <e4ting@qq.com> 2021-07-14 17:57:18
"""
import json
from flask_restful  import Resource
from flask          import redirect,request,make_response

from urllib.parse   import urlparse

from e4ting         import util,log
from core.cache     import RoomCache
from models.mysqls  import db,ModelRoom
from core.game.room import Room,GamePlayer

# from e4ting.task   import Anon

class WebRoomSeat(Resource):
    """ 座位 """

    def get(self, id=None):
        """ 单个房间座位列表 """
        if not id:
            return ApiErr("房间ID不对", code=400)
        room = Room(uid=id)
        if not room.cache.exists():
            return ApiErr("房间不存在", code=404)
        room.init()
        data = [ s.webdump() for s in room.seats]
        return ApiOK(data)

    def post(self, id=None):
        """ 坐下 """
        uid = request.user["uid"]
        seat = request.json.get("seat", "0")
        seat = int(seat)
        uid  = str(uid)
        if not uid:
            return ApiErr("玩家ID不对", code=400)

        if not id:
            return ApiErr("房间ID不对", code=400)

        room = Room(uid=id)
        if not room.cache.exists():
            return ApiErr("房间不存在", code=404)
        room.init()

        if not (0 <= seat < len(room.seats)):
            return ApiErr("座位不存在", code=404)

        s = room.seats[seat]
        if all([ not s.is_null(), s.get_player() != uid]):
            return ApiErr("座位已有人", code=400)

        p = GamePlayer(uid=uid)

        if not (seat := p.get_seat_record()):
            # 没有占过任何座位
            s.set_player(p)
        elif str(seat.room) != room.uid:
            return ApiErr("不可同时参与多个房间游戏", code=400)
        elif seat.number != s.number:
            return ApiErr("不可同时占多个座位", code=400)
        # p.set_room(room)
        return ApiOK({
                "seat": s.webdump(),
                "player": p.webdump(),
            })

    def delete(self, id=None):
        """ 禁用 """
        uid = request.user["uid"]
        seat = request.json.get("seat", "0")
        seat = int(seat)
        uid  = str(uid)
        if not uid:
            return ApiErr("玩家ID不对", code=400)

        if not id:
            return ApiErr("房间ID不对", code=400)

        if not room.cache.exists():
            return ApiErr("房间不存在", code=404)

        if not (0 <= seat < len(room.seats)):
            return ApiErr("座位不存在", code=404)

        s = room.seats[seat]
        if s.get_player() != uid:
            return ApiErr("你不在此座位", code=400)

        p = GamePlayer(uid=uid)
        if not s.unset_player():
            return ApiErr("禁用座位失败", code=500)

        return ApiOK(f"退出成功")

    def put(self, id=None):
        """ 站起 """
        uid = request.user["uid"]
        seat = request.json.get("seat", "0")
        seat = int(seat)
        uid  = str(uid)
        if not uid:
            return ApiErr("玩家ID不对", code=400)
        # if not seat:
        #     return ApiErr("座位号不对", code=400)

        if not id:
            return ApiErr("房间ID不对", code=400)
        room = Room(uid=id)
        if not room.cache.exists():
            return ApiErr("房间不存在", code=404)
        room.init()

        if not (0 <= seat < len(room.seats)):
            return ApiErr("座位不存在", code=404)

        s = room.seats[seat]
        if s.get_player() != uid:
            return ApiErr("你不在此座位", code=400)

        p = GamePlayer(uid=uid)
        if not s.unset_player():
            return ApiErr("站起失败", code=500)

        return ApiOK("站起成功")
